package org.spring.blog.controller.admin;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@PreAuthorize("@permission.admin()")
@RestController
@RequestMapping("/admin/image")
public class ImageAdminApi {

    @Autowired
    IImageService imageService;


    /**
     * 上传图片
     * 方法要求
     * 1.可以配置上传路径
     * 2.命名用id，按照每天一个文件夹 保存
     * 3.同步数据库
     *
     * @param _file 文件路径
     * @return 自定义返回对象
     */
    @ApiOperation("上传图片")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.file_cant_empty_code, message = ResponseState.file_cant_empty_message),
            @ApiResponse(code = ResponseState.file_type_error_code, message = ResponseState.file_type_error_message),
            @ApiResponse(code = ResponseState.file_type_no_support_code, message = ResponseState.file_type_no_support_message),
            @ApiResponse(code = ResponseState.file_size_too_big_code, message = ResponseState.file_size_too_big_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
            @ApiResponse(code = ResponseState.failed_code, message = ResponseState.failed_message),
    })
    @PostMapping("/{_original}")
    public ResponseResult uploadImage(@RequestParam("file") MultipartFile _file, @PathVariable String _original) {
        try {
            return imageService.uploadImage(_file, _original);
        } catch (IOException _e) {
            _e.printStackTrace();
            return ResponseResult.failed();
        }
    }

    /**
     * 删除图片
     *
     * @param _Id 图片id
     * @return 自定义返回对象
     */
    @ApiOperation("删除图片")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.file_no_have_code, message = ResponseState.file_no_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") String _Id) {
        return imageService.delete(_Id);
    }

    /**
     * 查找图片
     *
     * @param _url 图片id
     */
    @ApiOperation("查找图片")
    @GetMapping("/{url}")
    public void query(@PathVariable("url") String _url,
                      HttpServletResponse _response) {
        try {
            imageService.query(_url, _response);
        } catch (IOException _e) {
            _e.printStackTrace();
        }
    }


    /**
     * 更新图片
     *
     * @param _Id 图片id
     * @return 自定义返回对象
     */
    @ApiOperation("更新图片")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
            @ApiResponse(code = ResponseState.failed_code, message = ResponseState.failed_message),
    })
    @PutMapping("/{id}")
    public ResponseResult update(@PathVariable("id") String _Id) {
        return ResponseResult.success();
    }

    /**
     * 获取图片列表
     *
     * @param _page 页码
     * @param _size 页面大小
     * @return 自定义返回对象
     */
    @ApiOperation("获取图片列表")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.user_not_login_code, message = ResponseState.user_not_login_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/list/{_page}/{_size}/{_original}")
    public ResponseResult list(@PathVariable int _page,
                               @PathVariable int _size,
                               @PathVariable String _original) {
        return imageService.list(_page, _size, _original);
    }
}
